package com.zhangqi.zhxa;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;


import com.zhangqi.zhxa.utils.PrefUtils;

import java.util.ArrayList;

/**
 * 新手引导
 * @author Zhang Qi
 */
public class GuideActivity extends AppCompatActivity implements View.OnClickListener{

    private ViewPager mViewPager;//引导页面view
    private ImageView ivRedPoint;//小红点
    private Button btnStart;//开始按钮

    //引导图片3张
    private int [] mImageIds = new int [] {R.drawable.guide_1,
            R.drawable.guide_2,R.drawable.guide_3};

    private ArrayList<ImageView> mImageViewList;//设置集合

    private LinearLayout llContainer;//3个灰色点
    private int mPointWidth;//两个小圆点之间的宽度

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);

        mViewPager = (ViewPager) findViewById(R.id.vp_pager);
        llContainer = (LinearLayout) findViewById(R.id.ll_container);//3个灰色点
        final ImageView ivRedPoint = (ImageView) findViewById(R.id.iv_red_point);//小红点
        final Button btnStart = (Button) findViewById(R.id.btn_start);
        btnStart.setOnClickListener(this);//设置开始按钮监听

        //初始化ImageView,为了避免每次都new,占用内存
        mImageViewList = new ArrayList<ImageView>();

        for(int i=0;i<mImageIds.length;i++){
            ImageView view = new ImageView(this);
            view.setBackgroundResource(mImageIds[i]);
            mImageViewList.add(view);

            //初始化圆点
            ImageView pointView = new ImageView(this);
            pointView.setImageResource(R.drawable.shape_circle_default);
            //要设边距就要LayoutParams布局参数集合---初始化布局参数
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT
            );
            if(i>0){
                params.leftMargin=10;//从第二个点开始，设置margin为10
            }

            pointView.setLayoutParams(params);//放进来

            llContainer.addView(pointView);
        }

        mViewPager.setAdapter(new MyAdapter());
        //拿到视图树，再设置监听,监听我们视图树的变化
        ivRedPoint.getViewTreeObserver().addOnGlobalLayoutListener(
                new ViewTreeObserver.OnGlobalLayoutListener() {
            //layout方法执行结束（位置确定）
            @Override
            public void onGlobalLayout() {
                //取消监听,使只监听一次，以后不进来了，移除了监听
                ivRedPoint.getViewTreeObserver().removeGlobalOnLayoutListener(this);

                //页面绘制结束之后，计算两个圆点的间距,0是第一个圆点，1是第二个圆点，相减为点间距
                mPointWidth = llContainer.getChildAt(1).getLeft() -
                        llContainer.getChildAt(0).getLeft();
                //Log.i("aa","两个圆点的宽度------------------------------"+mPointWidth);
            }
        });

        //设置滑动监听
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            //页面滑动的过程中，positionOffset表示偏移的百分比
            @Override
            public void onPageScrolled(int position, float positionOffset,
                                       int positionOffsetPixels) {
                //Log.i("aa","当前位置："+position+"----偏移百分比："+positionOffset);
                //计算当前小红点的左边距
                //利用百分比计算左边距=移动距离+（0或1或2）*点距
                int leftMargin = (int) (mPointWidth*positionOffset+position*mPointWidth);

                //修改小红点的左边距
                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)
                        ivRedPoint.getLayoutParams();//利用它的父类布局参数
                params.leftMargin = leftMargin;//设置左边距等于我们计算出的左边距
                ivRedPoint.setLayoutParams(params);//设置给小红点
            }

            @Override
            public void onPageSelected(int position) {
                if(position==mImageIds.length-1){//最后页面显示开始体验按钮
                    btnStart.setVisibility(View.VISIBLE);
                }else{
                    btnStart.setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }



    class MyAdapter extends PagerAdapter{

        @Override
        public int getCount() {
            return mImageIds.length;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView view = mImageViewList.get(position);
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View)object);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_start:
                //开始体验,注意如果展现过了就不再展现了
                //记录新手引导已经被展示的状态，下次不再展示
                PrefUtils.putBoolean("is_guide_show", true, this);

                startActivity(new Intent(this, MainActivity.class));
                finish();
                break;

            default:
                break;
        }
    }
}
